package net.lb.io.json.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.lb.io.json.JsonArray;
import net.lb.io.json.JsonObject;
import net.lb.io.json.serializer.ArraySerializer;
import net.lb.io.json.serializer.BooleanSerializer;
import net.lb.io.json.serializer.CalendarSerializer;
import net.lb.io.json.serializer.CollectionSerializer;
import net.lb.io.json.serializer.DateToLongSerializer;
import net.lb.io.json.serializer.FieldContainerSerializer;
import net.lb.io.json.serializer.DoubleSerializer;
import net.lb.io.json.serializer.FloatSerializer;
import net.lb.io.json.serializer.IntegerSerializer;
import net.lb.io.json.serializer.JsonObjectSerializer;
import net.lb.io.json.serializer.LongSerializer;
import net.lb.io.json.serializer.MapSerializer;
import net.lb.io.json.serializer.ObjectSerializer;
import net.lb.io.json.serializer.ShortSerializer;
import net.lb.io.json.serializer.StringSerializer;

public class JsonConfig
{
    private static Map<Class<?>, JsonSerializer> serializerMap   = new ConcurrentHashMap<>();
    private static ArraySerializer               arraySerializer = new ArraySerializer();
    static
    {
        serializerMap.put(Object.class, new ObjectSerializer());
        // serializerMap.put(Date.class, new DateSerializer());
        serializerMap.put(Date.class, new DateToLongSerializer());
        serializerMap.put(Calendar.class, new CalendarSerializer());
        serializerMap.put(Boolean.class, new BooleanSerializer());
        serializerMap.put(String.class, new StringSerializer());
        serializerMap.put(Map.class, new MapSerializer());
        serializerMap.put(LinkedHashMap.class, new MapSerializer());
        serializerMap.put(HashMap.class, new MapSerializer());
        serializerMap.put(JsonObject.class, new JsonObjectSerializer());
        serializerMap.put(JsonArray.class, new CollectionSerializer());
        serializerMap.put(Collection.class, new CollectionSerializer());
        serializerMap.put(LinkedList.class, new CollectionSerializer());
        serializerMap.put(ArrayList.class, new CollectionSerializer());
        serializerMap.put(Double.class, new DoubleSerializer());
        serializerMap.put(Float.class, new FloatSerializer());
        serializerMap.put(Integer.class, new IntegerSerializer());
        serializerMap.put(Short.class, new ShortSerializer());
        serializerMap.put(Long.class, new LongSerializer());
    }
    
    public static JsonSerializer getSerializer(Class<?> type)
    {
        JsonSerializer jsonSerializer = serializerMap.get(type);
        if (jsonSerializer == null)
        {
            if (type.isArray())
            {
                return arraySerializer;
            }
            JsonAnalyser.analyse(type);
            return serializerMap.get(type);
        }
        else
        {
            return jsonSerializer;
        }
        
    }
    
    public static void putSerializer(Class<?> type, FieldContainerSerializer serializer)
    {
        serializerMap.put(type, serializer);
    }
    
    public static boolean containsSerializer(Class<?> type)
    {
        return serializerMap.containsKey(type);
    }
}
